package com.ebt.m.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * created for normal edit item
 * 
 * @author Andy.Wang
 *
 */
public class EditBaseItemView extends RelativeLayout implements OnClickListener {
	/**
	 * dentity
	 */
	private float mScale;
	@BindView(R.id.title_label)
	TextView title_label;
	@BindView(R.id.title_img)
	ImageView title_img;
	@BindView(R.id.item_img)
	CircleImageView item_img;
	@BindView(R.id.item_des)
	EditText item_des;
	OnViewClickListener mClickListener;
	private String titleLabel;
	private int titleImgResid;
	private boolean showDesImg;

	public EditBaseItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditBaseItemView, defStyle, 0);
		titleLabel = a.getString(R.styleable.EditBaseItemView_title_lable);
		titleImgResid = a.getResourceId(R.styleable.EditBaseItemView_title_img, 0);
		showDesImg = a.getBoolean(R.styleable.EditBaseItemView_show_des_img, false);
		a.recycle();
		inflate(context, R.layout.card_edit_item_layout, this);
		ButterKnife.bind(this);
		setClickable(true);
		setOnClickListener(this);
		initView();
	}

	private void initView() {
		setTitle(titleLabel);
		setTitleImage(titleImgResid);
		setShowDesImg(showDesImg);
		setBackgroundColor(Color.parseColor("#ffffff"));

	}

	public EditBaseItemView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public EditBaseItemView(Context context) {
		this(context, null);

	}

	public void setTitleImage(int resid) {
		if (resid == 0) {
			title_img.setVisibility(View.GONE);
			return;
		}
		title_img.setVisibility(View.VISIBLE);
		title_img.setBackgroundResource(resid);
	}

	public void setDesImage(Bitmap bitmap) {
		item_des.setVisibility(View.GONE);
		item_img.setVisibility(View.VISIBLE);
		if (bitmap == null) {
			return;
		}
		item_img.setImageBitmap(bitmap);
	}


	public void setTitle(String str) {
		title_label.setText(str);
	}

	public void setShowDesImg(boolean show) {
		if (show) {
			item_img.setVisibility(View.VISIBLE);
			item_des.setVisibility(View.GONE);
			item_img.setImageResource(R.drawable.ic_avatar);
		} else {
			item_img.setVisibility(View.GONE);
			item_des.setVisibility(View.VISIBLE);
		}
	}

	public void setDes(String str) {
		item_des.setVisibility(View.VISIBLE);
		item_img.setVisibility(View.GONE);
		if (str == null) {
			return;
		}
		item_des.setText(str);
	}

	public void setClickListener(OnViewClickListener listener) {
		mClickListener = listener;
	}

	public interface OnViewClickListener {
		void onClick(String title);
	}

	@Override
	public void onClick(View v) {
		if (mClickListener != null) {
			mClickListener.onClick(title_label.getText().toString().trim());
		}
	}

}
